/**
 * @author lilongkang
 * @version 1.0
 * @description: TODO
 * @date 2024/3/14 21:07
 */
public class ArrayQuery {

    public static void main(String[] args) {
        int[] test1 = new int[]{2, 3, 1, 2, 4, 3};
        int[] test2 = new int[]{1, 1, 1, 1, 1, 1, 1, 1};
        System.out.println(query(test1, 7));
        System.out.println(query(test2, 11));
    }

    public static int query(int[] array, int target) {
        int size = array.length;
        int result = 0;
        for (int slow = 0; slow < size; slow++) {
            int temp = array[slow];
            for (int fast = slow + 1; fast < size; fast++) {
                temp = temp + array[fast];
                if (temp >= target) {
                    int num = fast - slow + 1;
                    if (num >= 0 && result == 0) {
                        result = num;
                    } else {
                        result = Math.min(result, num);
                    }
                    break;
                }
            }
        }
        return result;
    }

    // 滑动窗口解法
    public int minSubArrayLen(int s, int[] nums) {
        int left = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= s) {
                result = Math.min(result, right - left + 1);
                sum -= nums[left++];
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}
